package com.ken.apigateway.utils;

import com.ken.apigateway.comm.Const;
import com.ken.apigateway.comm.CustomProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class CookieUtil {

    @Autowired
    private CustomProperty customProperty;
    private static CookieUtil cookieUtil;

    @PostConstruct
    public void init() {
        cookieUtil = this;
    }

    public static String readLoginToken(HttpServletRequest request, String cookieName){
        Cookie[] cookies = request.getCookies();
        if (cookies!=null){
            for (Cookie cookie : cookies){
                log.debug("read cookieName:{},cookieValue:{}",cookie.getName(),cookie.getValue());
                if (StringUtils.equals(cookie.getName(),cookieName)){
                    log.debug("return cookieName:{},cookieValue:{}",cookie.getName(),cookie.getValue());
                    return cookie.getValue();
                }
            }
        }
        return null;
    }


    public static void writeLoginToken(HttpServletRequest request, HttpServletResponse response, String token,String cookieName){
        writeLoginToken(request,response,token, cookieName,cookieUtil.customProperty.getCookieDomain());
    }

    public static void writeLoginToken(HttpServletRequest request, HttpServletResponse response, String token, String cookieName, String cookieDomain){
        //注意，先覆盖再追加
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie: cookies){
                if (StringUtils.equals(cookie.getName(),cookieName)){
                    cookie.setDomain(cookieDomain);
                    cookie.setPath("/");
                    cookie.setMaxAge(Const.CookieExtime.COOKIE_EXTIME);
                    log.debug("overwrite cookieName:{}'s cookieValue cookieValue:{} to newCookieValue{}",cookie.getName(),cookie.getValue(),token);
                    cookie.setValue(token);
                    response.addCookie(cookie);
                }
            }
        }
        Cookie cookie = new Cookie(cookieName,token);
        cookie.setDomain(cookieUtil.customProperty.getCookieDomain());
        cookie.setPath("/");
        cookie.setHttpOnly(true);//防止XSS攻击获取cookie
        cookie.setMaxAge(Const.CookieExtime.COOKIE_EXTIME);//单位是秒、此项不设置则只写入内存，设置-1代表永久有效
        log.debug("write cookieName:{},cookieValue:{}",cookie.getName(),cookie.getValue());
        response.addCookie(cookie);
    }

    public static void delLoginToken(HttpServletRequest request, HttpServletResponse response, String cookieName){
        delLoginToken(request, response, cookieName, cookieUtil.customProperty.getCookieDomain());
    }
    public static void delLoginToken(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieDomain){
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie: cookies){
                if (StringUtils.equals(cookie.getName(),cookieName)){
                    cookie.setDomain(cookieDomain);
                    cookie.setPath("/");
                    cookie.setMaxAge(-1);
                    log.debug("del cookieName:{},cookieValue:{}",cookie.getName(),cookie.getValue());
                    response.addCookie(cookie);
                }
            }
        }
    }
}
